#include "ParticleResultData.h"

ParticleResultData::ParticleResultData(const shared_ptr<ParticleTracking> mainParticleTracking0)
{
	int np = (*mainParticleTracking0).totalP;
	pVx = vector<vector<double>>(np, vector<double>());
	pVy = vector<vector<double>>(np, vector<double>());
	pX = vector<vector<double>>(np, vector<double>());
	pY = vector<vector<double>>(np, vector<double>());
	pTime = vector<vector<double>>(np, vector<double>());
	for (int i = 0; i < np; i++)
	{
		pDia.push_back((*(*mainParticleTracking0).pAll_s)[i].diam);
		int nt = (*(*mainParticleTracking0).PAll_allTime)[i].size();
		for (int j = 0; j < nt; j++)
		{
			pVx[i].push_back((*(*mainParticleTracking0).PAll_allTime)[i][j].V[0]);
			pVy[i].push_back((*(*mainParticleTracking0).PAll_allTime)[i][j].V[1]);
			pX[i].push_back((*(*mainParticleTracking0).PAll_allTime)[i][j].pos[0]);
			pY[i].push_back((*(*mainParticleTracking0).PAll_allTime)[i][j].pos[1]);
			pTime[i].push_back((*(*mainParticleTracking0).PAll_allTime)[i][j].time);
		}
	}
}

void ParticleResultData::WriteToFile()
{

	/* filename variables write*/
    string filename = "locaParticleTrackingResults.txt";
	string localDirect = GetOutputDirectory(filename);
    ofstream write_results(localDirect);
	
	assert(write_results.is_open());

	int length = pDia.size();

	for (int i = 0; i < length; i++)
	{
		write_results.precision(10);
		if (i == 0)
		{
			write_results << "This is used to output particle tracking results for postprocessing!" << endl;
			write_results << "particle-ID" << "," << "V[0]" << "," << "V[1]" << "," << "pos[0]" << "," << "pos[1]"
				<< "," << "diam" << ","  << "time" << "\n";
		}
		
		int nt = pVx[i].size();

		for (int j = 0; j < nt; j++)
		{
			write_results << i + 1 << "," << pVx[i][j] << "," << pVy[i][j] << "," << pX[i][j] << "," << pY[i][j]
				<< "," << pDia[i] << "," << pTime[i][j] << "\n";
		}
	}
	write_results.close();
}

string ParticleResultData::GetOutputDirectory(string fileName)
{
	char homeDirect[1024];
	_getcwd(homeDirect, 1024);
	string localDirect = (string)(homeDirect)+(string)("\\LocalResults\\") + fileName;
	return localDirect;
}